package com.kedll.library.util;

/**
 * 对android的Log进行了封装，从而更便捷的处理日志，控制系统性能
 */
public final class Log {
    private static String defaultTag = Log.class.getSimpleName();

    public static final int LEVEL_NONE = 0;
    public static final int LEVEL_VERBOSE = 1;
    public static final int LEVEL_DEBUG = 2;
    public static final int LEVEL_INFO = 3;
    public static final int LEVEL_WARN = 4;
    public static final int LEVEL_ERROR = 5;
    public static final int LEVEL_FATAL = 6;
    public static final int LEVEL_ALL = 7;

    private static int filterLevel = LEVEL_NONE;

    private Log() {
    }

    public static int getFilterLevel() {
        return filterLevel;
    }

    public static void setFilterLevel(int filterLevel) {
        Log.filterLevel = filterLevel;
    }

    public static void setTag(String tag) {
        defaultTag = tag;
    }

    public static int i(Object o) {
        return filterLevel < LEVEL_INFO && o != null ? android.util.Log.i(defaultTag, o.toString()) : -1;
    }

    public static int i(String m) {
        return filterLevel < LEVEL_INFO && m != null ? android.util.Log.i(defaultTag, m) : -1;
    }

    /**
     * ******************** Log **************************
     */
    public static int v(String tag, String msg) {
        return filterLevel < LEVEL_VERBOSE && msg != null ? android.util.Log.v(tag, msg) : -1;
    }

    public static int d(String tag, String msg) {
        return filterLevel < LEVEL_DEBUG && msg != null ? android.util.Log.d(tag, msg) : -1;
    }

    public static int i(String tag, String msg) {
        return filterLevel < LEVEL_INFO && msg != null ? android.util.Log.i(tag, msg) : -1;
    }

    public static int w(String tag, String msg) {
        return filterLevel < LEVEL_WARN && msg != null ? android.util.Log.w(tag, msg) : -1;
    }

    public static int e(String tag, String msg) {
        return filterLevel < LEVEL_ERROR && msg != null ? android.util.Log.e(tag, msg) : -1;
    }

    /**
     * ******************** Log with object list **************************
     */
    public static int v(String tag, Object... msg) {
        return filterLevel < LEVEL_VERBOSE ? android.util.Log.v(tag, getLogMessage(msg)) : -1;
    }

    public static int d(String tag, Object... msg) {
        return filterLevel < LEVEL_DEBUG ? android.util.Log.d(tag, getLogMessage(msg)) : -1;
    }

    public static int i(String tag, Object... msg) {
        return filterLevel < LEVEL_INFO ? android.util.Log.i(tag, getLogMessage(msg)) : -1;
    }

    public static int w(String tag, Object... msg) {
        return filterLevel < LEVEL_WARN ? android.util.Log.w(tag, getLogMessage(msg)) : -1;
    }

    public static int e(String tag, Object... msg) {
        return filterLevel < LEVEL_ERROR ? android.util.Log.e(tag, getLogMessage(msg)) : -1;
    }

    private static String getLogMessage(Object... msg) {
        if (msg != null && msg.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (Object s : msg) {
                if (msg != null) sb.append(s.toString());
            }
            return sb.toString();
        }
        return "";
    }

    /**
     * ******************** Log with Throwable **************************
     */
    public static int v(String tag, String msg, Throwable tr) {
        return filterLevel < LEVEL_VERBOSE && msg != null ? android.util.Log.v(tag, msg, tr) : -1;
    }

    public static int d(String tag, String msg, Throwable tr) {
        return filterLevel < LEVEL_DEBUG && msg != null ? android.util.Log.d(tag, msg, tr) : -1;
    }

    public static int i(String tag, String msg, Throwable tr) {
        return filterLevel < LEVEL_INFO && msg != null ? android.util.Log.i(tag, msg, tr) : -1;
    }

    public static int w(String tag, String msg, Throwable tr) {
        return filterLevel < LEVEL_WARN && msg != null ? android.util.Log.w(tag, msg, tr) : -1;
    }

    public static int e(String tag, String msg, Throwable tr) {
        return filterLevel < LEVEL_ERROR && msg != null ? android.util.Log.e(tag, msg, tr) : -1;
    }

    /**
     * ******************** TAG use Object Tag **************************
     */
    public static int v(Object tag, String msg) {
        return filterLevel < LEVEL_VERBOSE ? android.util.Log.v(tag.getClass().getSimpleName(), msg) : -1;
    }

    public static int d(Object tag, String msg) {
        return filterLevel < LEVEL_DEBUG ? android.util.Log.d(tag.getClass().getSimpleName(), msg) : -1;
    }

    public static int i(Object tag, String msg) {
        return filterLevel < LEVEL_INFO ? android.util.Log.i(tag.getClass().getSimpleName(), msg) : -1;
    }

    public static int w(Object tag, String msg) {
        return filterLevel < LEVEL_WARN ? android.util.Log.w(tag.getClass().getSimpleName(), msg) : -1;
    }

    public static int e(Object tag, String msg) {
        return filterLevel < LEVEL_ERROR ? android.util.Log.e(tag.getClass().getSimpleName(), msg) : -1;
    }

    public static String getLogUtilsTag(Class<?> cls) {
        return cls.getSimpleName();
    }
}